from myelin.rdf.Ns import RDFS_SEEALSO
from myelin.rdf.bindery.Model import OBJECT_TYPE_RESOURCE
from nodetypes.ntypes.Document.showBase.RDF import Transform, Document


class Document(Document):
    
    containsKey = 'mContains'
        
    def getContainerStatements(self, context, consumer, driver, locale):
        #root = context.defaults.document_root
        
        gmap = set()
        gas = self.getAgentStatements
        href = context.urlFormatter.newUrlMapper(True)
        
        def nfilter(n):
            ng = n.mGrouping
            if not ng: return True
            for ng in ng:
                if not ng.mHidden:
                    return True
            return False
        
        # order nodes into their groupings
        for n in self.filterContainedNodes(
            context,
            consumer,
            [],
            driver,
            locale,
            nfilter
        ):            
            ng = n.rdf_toRelations('mGrouping')
            if ng:
                for gp,g in ng:
                    if g.mHidden:
                        continue
                    
                    consumer(
                        g.rdf_asStatement(
                            gp,
                            n.rdf_value
                        )
                    )
                    
                    gid = g.rdf_value
                    if gid not in gmap:
                        gmap.add(gid)
                        
                        grs = g.rdf_toStatements
                        consumer(grs('rdfType'))
                        consumer(grs('mTag'))
                        consumer(grs('dcTitle'))
                        consumer(grs('dcDescription'))
                        consumer(grs('dcLanguage'))
                        
                        o = href(g.rdf_domain)
                        if o:
                            consumer((
                                (
                                    gid,
                                    RDFS_SEEALSO,
                                    o,
                                    '',
                                    g.rdf_domain,
                                    OBJECT_TYPE_RESOURCE,
                                ),
                            ))
                    
            nrs = n.rdf_toStatements
            consumer(nrs('rdfType'))
            consumer(nrs('mTag'))
            consumer(nrs('dcTitle'))
            consumer(nrs('dcDescription'))
            consumer(nrs('dcLanguage'))
            
            consumer(nrs('dctermsCreated'))
            consumer(nrs('dcCreator'))
            for m in n.dcCreator:
                gas(context, consumer, m, locale) #, root)
            
            consumer(nrs('dctermsModified'))
            consumer(nrs('mLast_Modifier'))
            for m in n.mLast_Modifier:
                gas(context, consumer, m, locale) #, root)
            
            o = href(n.rdf_domain)
            if o:
                consumer((
                    (
                        n.rdf_value,
                        RDFS_SEEALSO,
                        o,
                        '',
                        n.rdf_domain,
                        OBJECT_TYPE_RESOURCE,
                    ),
                ))
        
        return consumer
    
    
class Transform(Transform):
    
    newDocument = Document